package com.bhprod.comatule.slideshow;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bhprod.comatule.R;
import com.bhprod.comatule.model.Photo;
import com.bhprod.comatule.model.Species;
import com.bhprod.comatule.util.Constants;
import com.bhprod.comatule.util.Tools;

public class SlideshowFragment extends Fragment {

	private Species species;
	private Context context;
//	private boolean siblingMinusTwo = false;
	private boolean siblingMinusOne = false;
//	private boolean siblingPlusTwo = false;
	private boolean siblingPlusOne = false;

//	public void setSiblingMinusTwo(boolean siblingMinusTwo) {
//		this.siblingMinusTwo = siblingMinusTwo;
//	}

	public void setSiblingMinusOne(boolean siblingMinusOne) {
		this.siblingMinusOne = siblingMinusOne;
	}

//	public void setSiblingPlusTwo(boolean siblingPlusTwo) {
//		this.siblingPlusTwo = siblingPlusTwo;
//	}

	public void setSiblingPlusOne(boolean siblingPlusOne) {
		this.siblingPlusOne = siblingPlusOne;
	}

	public void setContext(Context context) {
		this.context = context;
	}

	public SlideshowFragment() {
		super();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.slideshow_fragment, container, false);

		RelativeLayout layout = (RelativeLayout) rootView.findViewById(R.id.speciesLayout);
		layout.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Log.d(SlideshowFragment.class.getName(), "IMAGE CLICKED");
				((SlideshowActivity) context).displayDetail(null);
			}
		});

		TextView tv = (TextView) rootView.findViewById(R.id.speciesName);
		tv.setText(species.getName());
		tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);

		String filename;

		List<Photo> photos = species.getPhotos();
		
		if (photos != null && photos.size() > 0) {
			// We choose the first photo
			String idpath = Tools.completeIdWithZeros(species.getPhotos().get(0).getId(),
					getResources().getConfiguration().locale);
	
			filename = Constants.PHOTOS_PATH + idpath + Constants.PHOTOS_EXTENSION;
		} else {
			filename = Constants.PHOTOS_PATH + Constants.PHOTOS_DEFAULT;
		}

		try {
			InputStream path = context.getAssets().open(filename);
			Bitmap bit = BitmapFactory.decodeStream(path);

			ImageView image = (ImageView) rootView.findViewById(R.id.speciesImage);
			image.setImageBitmap(bit);
			image.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					Log.d(SlideshowFragment.class.getName(), "IMAGE CLICKED");
					((SlideshowActivity) context).displayDetail(null);
				}
			});
		} catch (IOException e) {
			Log.e(SlideshowFragment.class.getName(), "Cant't find file " + filename, e);
		}

//		if (!siblingMinusTwo) {
//			ImageView icon = (ImageView) rootView.findViewById(R.id.cursorImageMinusTwo);
//			icon.setVisibility(View.INVISIBLE);
//		}
		if (!siblingMinusOne) {
			ImageView icon = (ImageView) rootView.findViewById(R.id.cursorImageMinusOne);
			icon.setVisibility(View.INVISIBLE);
		}
		if (!siblingPlusOne) {
			ImageView icon = (ImageView) rootView.findViewById(R.id.cursorImagePlusOne);
			icon.setVisibility(View.INVISIBLE);
		}
//		if (!siblingPlusTwo) {
//			ImageView icon = (ImageView) rootView.findViewById(R.id.cursorImagePlusTwo);
//			icon.setVisibility(View.INVISIBLE);
//		}

		return rootView;
	}

	public Species getSpecies() {
		return species;
	}

	public void setSpecies(Species species) {
		this.species = species;
	}

}
